package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.BdActLeave;
import com.example.demo.entity.BdActivity;
import com.example.demo.mapper.ActLeaveMapper;
import com.example.demo.mapper.ActivityMapper;
import com.example.demo.service.ActLeaveService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/leave")
public class ActLeaveController extends BaseController{

    @Resource
    ActLeaveMapper actLeaveMapper;

    @Resource
    ActivityMapper activityMapper;

    @Autowired
    ActLeaveService actLeaveService;

    @PostMapping
    public Result<?> save(@RequestBody BdActLeave BdActLeave) {
//        if(StrUtil.isNotBlank(ActLeave.getActivityId())){
//            actLeaveMapper.insert(ActLeave);
//        }else{
//            actLeaveMapper.updateById(ActLeave);
//        }
        actLeaveMapper.insert(BdActLeave);
        LambdaQueryWrapper<BdActivity> wrapper = Wrappers.<BdActivity>lambdaQuery();
        wrapper.eq(BdActivity::getId, BdActLeave.getActivityId());
        BdActivity bdActivity =activityMapper.selectOne(wrapper);
        bdActivity.setIsHaveLeave( true);
        activityMapper.updateById(bdActivity);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody BdActLeave BdActLeave) {
        actLeaveMapper.updateById(BdActLeave);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        actLeaveMapper.deleteById(id);
        return Result.success();
    }

//    @PostMapping("/deleteBatch")
//    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
//        activityMapper.deleteBatchIds(ids);
//        return Result.success();
//    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(actLeaveMapper.selectByActiveId(id));
    }

//    @GetMapping
//    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
//                              @RequestParam(defaultValue = "10") Integer pageSize,
//                              @RequestParam(defaultValue = "") String name,
//                              @RequestParam(defaultValue = "") String startDate,
//                              @RequestParam(defaultValue = "") String endDate) {
//        LambdaQueryWrapper<ActLeave> wrapper = Wrappers.<ActLeave>lambdaQuery();
//        Page<Activity> activityPage = actLeaveMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
//        return Result.success(activityPage);
//    }
    /**
     * 导出案例
     *
     * @param id
     * @return RestResponse
     */
    @ApiOperation(value = "导出撤场单", notes = "导出撤场单")
    @GetMapping(value = "/export/{id}")
    public void export(@PathVariable Integer id, HttpServletResponse response) throws Exception {
        BdActLeave bdActLeave =actLeaveMapper.selectByActiveId(id);
        actLeaveService.exportToPdf(bdActLeave, response);
    }
}
